<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

use app\lib\baidu\Api as baiduApi;


class Plan extends Controller
{
    public function zhuaqu(){
        header('Content-Type: text/html; charset=utf-8');

        for ($i=1; $i <= 9; $i++) { 
            $url = 'https://shop.fantasy-village.com/shopbrand/ct77/page'.$i.'/recommend/';
            $content = file_get_contents($url);
            $content = mb_convert_encoding($content, 'UTF-8', 'EUC-JP');

            preg_match_all('/<div class="category-list-inner">(.*?)<a href=JavaScript:login/is', $content, $matches);

            $items = [];

            foreach ($matches[1] as $match) {
                $item = [];

                // 匹配 URL
                preg_match('/<a href="([^"]+)"/i', $match, $urlMatch);
                $item['url'] = isset($urlMatch[1]) ? $urlMatch[1] : '';

                // 匹配 name (alt 属性)
                preg_match('/<img alt="([^"]+)"/i', $match, $nameMatch);
                $item['name'] = isset($nameMatch[1]) ? $nameMatch[1] : '';

                // 匹配 image (src 属性)
                preg_match('/<img[^>]+src="([^"]+)"/i', $match, $imageMatch);
                $item['image'] = isset($imageMatch[1]) ? $imageMatch[1] : '';

                // 匹配 price
                preg_match('/<em class="price">([^<]+)<\/em>/i', $match, $priceMatch);
                $item['price'] = isset($priceMatch[1]) ? trim($priceMatch[1]) : '';

                $items[] = $item;
            }

            foreach ($items as $k => $vo) {
                Db::name('goods')->insert([
                    'description'=>$vo['url'],
                    'name'=>$vo['name'],
                    'cover'=>$vo['image'],
                    'ctime'=>time(),
                    'price'=>$vo['price']
                ]);
            }
            
        }
    }


    public function zhuaqu2(){
        header('Content-Type: text/html; charset=utf-8');
        $list = Db::name('goods')->where('status',0)->select();
        foreach ($list as $i => $item) {
            $url = 'https://shop.fantasy-village.com/'.$item['description'];
            $content = file_get_contents($url);
            $content = mb_convert_encoding($content, 'UTF-8', 'EUC-JP');

            preg_match('/<div class="item-wrap-top">(.*?)<script language=\'javascript\'>/s', $content, $matchesContent);
            $content = $matchesContent[0];

            // 使用正则表达式匹配需要的内容
            preg_match('/<div class="item-detail-text">(.*?)<\/div>/s', $content, $matchesContent);
            $content2 = isset($matchesContent[1]) ? trim(strip_tags($matchesContent[1])) : '';

            preg_match('/<div class="M_imageMainWrap">(.*?)<div class="M_imageThumbnail">/s', $content, $matchesContent);
            $content = $matchesContent[1];
            preg_match_all('/<img[^>]+src="([^"]+)"[^>]*>/i', $content, $matchesImages);
            $images = implode(',', array_map('trim', $matchesImages[1]));

            // preg_match('/<div class="item-detail-price"><span class="icon-yen">JPY￥<\/span>(.*)/i', $content, $matchesPrice);
            // $price = isset($matchesPrice[1]) ? (int)$matchesPrice[1] : 0;

            // 创建数组
            $result = [
                'content' => $content2,
                'images' => $images,
            ];
            if (!empty($result['content']) && !empty($result['images'])) {
                $result['status'] = 1;
            }
            
            Db::name('goods')->where('id',$item['id'])->update($result);
            
        }
    }

    public function uploadCover(){
        $list = Db::name('goods')->where('status',1)->select();
        foreach ($list as $k => $vo) {
            $cover = file_get_contents($vo['cover']);
            $ext = explode('.',$vo['cover']);
            $ext = $ext[count($ext) - 1];

            $filename = uuidCreate();
            file_put_contents('./uploads/'.$filename.'.'.$ext, $cover);

            $result = uploadFile($filename.'.'.$ext,'./uploads/'.$filename.'.'.$ext,'overshot/');
            if ($result['status'] == 1) {
                Db::name('goods')->where('id',$vo['id'])->update([
                    'cover'=>$result['data'],
                    'status'=>2
                ]);
            }
        }

    }

    public function uploadImages(){
        $list = Db::name('goods')->where('status',2)->select();
        $types = ["jpg", "gif", 'png', 'bmp','jpeg'];
        foreach ($list as $k => $vo) {
            $images = explode(',',$vo['images']);

            $save_images = [];
            foreach ($images as $image) {
                $ext = explode('?',$image);
                $ext = $ext[0];
                $ext = explode('.',$ext);
                $ext = $ext[count($ext) - 1];
                if (!in_array($ext, $types)) {
                    continue;
                }
                $save_file = file_get_contents($image);
                $filename = uuidCreate();
                $dd = file_put_contents('./uploads/'.$filename.'.'.$ext, $save_file);
                if (empty($dd)) {
                    continue;
                }

                $result = uploadFile($filename.'.'.$ext,'./uploads/'.$filename.'.'.$ext,'overimage/');
                if ($result['status'] == 1) {
                    $save_images[] = $result['data'];
                    
                }
            }
            if (!empty($save_images)) {
                Db::name('goods')->where('id',$vo['id'])->update([
                    'images'=>implode(',',$save_images),
                    'status'=>3
                ]);
            }
            // dump($save_images);die;
        }

    }
}
